//
//  XMTextView.m
//  DATABK
//
//  Created by 俞渊华 on 2017/3/31.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "XMTextView.h"

@interface XMTextView ()
///显示多少文字的Label
@property(nonatomic, strong)UILabel *numberLabel;
@property (strong, readwrite, nonatomic) UILabel *placeholderLabel;


@end

@implementation XMTextView
- (void)awakeFromNib{
    [super awakeFromNib];
    self.placeholder = @"";
    self.placeholderColor = [UIColor lightGrayColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
}

- (id)initWithFrame:(CGRect)frame{
    if ((self = [super initWithFrame:frame])) {
        self.placeholder = @"";
        self.placeholderColor = [UIColor lightGrayColor];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}

-(void)setMaxNumber:(NSInteger)maxNumber{
    _maxNumber = maxNumber;
    if (_maxNumber > 0) {
        self.numberLabel.font = self.font;
        self.numberLabel.textColor = self.placeholderColor;
        self.numberLabel.text = [NSString stringWithFormat:@"0/%ld",(long)_maxNumber];
        [self addSubview:self.numberLabel];
    }
}

- (void)textChanged:(NSNotification *)notification{
    if (self.placeholder.length == 0){
        return;
    }
    if (self.maxNumber > 0) {
        if (self.text.length >0) {
            [self.placeholderLabel setHidden:YES];
        }
        
        if (self.text.length >self.maxNumber && self.maxNumber != 0 && self.markedTextRange == nil) {
            self.text = [self.text substringToIndex:self.maxNumber];
        }
        self.numberLabel.text = [NSString stringWithFormat:@"%ld/%ld",(long)[self.text length],(long)self.maxNumber];
        [self.numberLabel sizeToFit];
    }
    
    if (self.text.length == 0) {
        [[self viewWithTag:999] setAlpha:1];
    } else {
        [[self viewWithTag:999] setAlpha:0];
    }
}

-(UILabel *)numberLabel{
    if (!_numberLabel) {
        _numberLabel = [[UILabel alloc]init];
        _numberLabel.textAlignment = NSTextAlignmentRight;
    }
    return _numberLabel;
}

- (void)setText:(NSString *)text{
    [super setText:text];
    [self textChanged:nil];

}
-(void)layoutSubviews{
    [super layoutSubviews];
    if (self.maxNumber > 0) {
        [self.numberLabel sizeToFit];
        self.numberLabel.frame = CGRectMake(self.frame.size.width - self.numberLabel.frame.size.width-5,
                                            self.frame.size.height-self.numberLabel.frame.size.height-5,
                                            self.numberLabel.frame.size.width,
                                            self.numberLabel.frame.size.height);
        self.contentInset = UIEdgeInsetsMake(0, 0, self.numberLabel.frame.size.height, 0);

//        NSLog(@"numberLabel.frame == %@",NSStringFromCGRect(self.numberLabel.frame));
    }
}
- (void)drawRect:(CGRect)rect{
    if (self.placeholder.length > 0) {
        if (!self.placeholderLabel) {
            self.placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 8, self.bounds.size.width - 10, 0)];
            self.placeholderLabel.numberOfLines = 0;
            self.placeholderLabel.font = self.font;
            self.placeholderLabel.backgroundColor = [UIColor clearColor];
            self.placeholderLabel.textColor = self.placeholderColor;
            self.placeholderLabel.alpha = 0;
            self.placeholderLabel.tag = 999;
            [self addSubview:self.placeholderLabel];
        }
        
        self.placeholderLabel.textAlignment = self.textAlignment;
        self.placeholderLabel.text = self.placeholder;
        [self.placeholderLabel sizeToFit];
        [self sendSubviewToBack:self.placeholderLabel];
        

    }
    
    if (self.text.length == 0 && self.placeholder.length > 0) {
        [[self viewWithTag:999] setAlpha:1];
    }
    
    [super drawRect:rect];
}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
